﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

namespace Crm.Web
{
    public enum ProgramTipi
    {
        Local, Mikro
    }

    public class QueryHelper : IDisposable
    {
        public QueryHelper(ProgramTipi tip)
        {
            if (tip == ProgramTipi.Local)
            {
                ConnString = WebConfigurationManager.ConnectionStrings["localdb"].ConnectionString;
            }
            else
            {
                ConnString = WebConfigurationManager.ConnectionStrings["mikrodb"].ConnectionString;
            }
        }
        string ConnString = "";

        public void ExecuteNonQuery(string sqlText, params SqlParameter[] param)
        {
            using (SqlConnection cn = new SqlConnection(ConnString))
            {
                cn.Open();

                using (SqlCommand cmd = new SqlCommand(sqlText, cn))
                {
                    if (param != null)
                    {
                        foreach (SqlParameter p in param)
                        {
                            cmd.Parameters.Add(p);
                        }
                    }
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                }
                cn.Close();
            }
        }

        public DataTable Fill(string sqlText, params SqlParameter[] param)
        {
            DataTable dt = new DataTable();
            using (SqlConnection cn = new SqlConnection(ConnString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(sqlText, cn))
                {
                    cmd.CommandTimeout = int.MaxValue;
                    if (param != null)
                    {
                        foreach (SqlParameter p in param)
                        {
                            cmd.Parameters.Add(p);
                        }
                    }

                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        adp.Fill(dt);
                    }
                    cmd.Parameters.Clear();
                }
                cn.Close();
            }
            return dt;
        }

        public SqlDataReader ExecuteReader(string sqlText, params SqlParameter[] param)
        {
            SqlConnection cn = new SqlConnection(ConnString);
            cn.Open();
            SqlCommand cmd = new SqlCommand(sqlText, cn);
            if (param != null)
            {
                if (param != null)
                {
                    foreach (SqlParameter p in param)
                    {
                        cmd.Parameters.Add(p);
                    }
                }
            }
            return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        }

        public T ExecuteScalar<T>(string sqlText, params SqlParameter[] param)
        {
            object sonuc = null;
            using (SqlConnection cn = new SqlConnection(ConnString))
            {
                cn.Open();

                using (SqlCommand cmd = new SqlCommand(sqlText, cn))
                {
                    if (param != null)
                    {
                        foreach (SqlParameter p in param)
                        {
                            cmd.Parameters.Add(p);
                        }
                    }
                    sonuc = cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                }
                cn.Close();
            }
            return (T)Convert.ChangeType(sonuc, typeof(T));

        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
}